layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table   
    ,admin = layui.admin;
    
     //角色管理
  table.render({
    elem: '#LAY-user-back-role'
    ,url: "/api/admin/role/page" //模拟接口
    ,cols: [[
      {type: 'numbers',title: '序号', width: 80,fixed: 'left'}
      ,{field: 'name', title: '角色名'}
      ,{field: 'enname', title: '英文名称'}
      ,{field: 'roleType', title: '角色类型'}
      ,{field: 'remarks', title: '具体描述'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
    ]],
    page: true
    ,text: '对不起，加载出现异常！'
  });



  
 //监听工具条
 table.on('tool(LAY-user-back-role)', function(obj){
  var data = obj.data;
  if(obj.event === 'del'){
    active.del(data.id);
  } else if(obj.event === 'edit'){
    active.edit(data.id);
  } else if(obj.event === 'add'){
    active.add();
  } 
});

$('#add').click(function () {
  active.add();
});

  //搜索角色
  form.on('submit(LAY-user-back-role_find)', function(data){
    //执行重载
    table.reload('LAY-user-back-role', {
      where: data.field
    });
  });

  //事件
  var active = {
    del: function(id){   
      layer.confirm('确定删除吗？', function(index) {
          delData(id, index);
      });
    },
    add: function(){
      updateOrAdd("添加角色", "");
    },
    edit:function(id){
      updateOrAdd("编辑角色", id);
    }
  }  

 
  function updateOrAdd(title, id){
    layer.open({
      type: 2
      ,title: title
      ,content: 'roleForm.html#/id='+id
      ,area: ['500px', '480px']
      ,btn: ['确定', '取消']
      ,yes: function(index, layero){
        var iframeWindow = window['layui-layer-iframe'+ index]
        ,submit = layero.find('iframe').contents().find("#LAY-role-submit");

        //监听提交
        iframeWindow.layui.form.on('submit(LAY-role-submit)', function(data){
          var field = data.field; //获取提交的字段
          //提交 Ajax 成功后，静态更新表格中的数据
         if(field.id != undefined && field.id != ""){
            subUpdateOrSave(field,"PUT", index);
         } else{
            delete field.id;
            subUpdateOrSave(field,"POST", index);
         }
         
        });  
        
        submit.trigger('click');
      }
      ,success: function(layero, index){
      
      }
    })
  }



  //修改或者添加
  function subUpdateOrSave(data, method, index){
    layer.close(index); //关闭弹层
    data._method =method;
    admin.req({
      type: method,
      url:"/api/admin/role",  
      data: JSON.stringify(data),   
      dataType:"json",
      contentType:'application/json;charset=utf8',
      success:function(res){
          // 表示是修改数据
          var data = res.data;
          if(res.code==200){
            table.reload('LAY-user-back-role'); //数据刷新
          }
      },
      error:function(jqXHR){
          alert("发生错误："+ jqXHR.status);
      }
    });
  }

  function delData(id, index){
    layer.close(index); //关闭弹层
    admin.req({
      type: "DELETE",
      url:"/api/admin/role/"+id,  
      dataType:"json",
      contentType:'application/json;charset=utf8',
      success:function(res){
          var data = res.data;
          if(res.code==200){
            table.reload('LAY-user-back-role'); //数据刷新
          }
      },
      error:function(jqXHR){
          alert("发生错误："+ jqXHR.status);
      }
    });
  }


 
  });